home *** CD-ROM | disk | FTP | other *** search
- Path: telepost.no!usenet
- From: Carsten Arnholm <ca@sesam.dnv.no>
- Newsgroups: comp.lang.c++,comp.lang.fortran
- Subject: Re: Calling Fortran in C++
- Date: 9 Jan 1996 13:48:54 GMT
- Organization: DNV
- Message-ID: <4ctro6$28n@nms.telepost.no>
- References: <4cr87l$794$1@mhafc.production.compuserve.com> <4crisr$v3u@odin.sunquest.com>
- NNTP-Posting-Host: hugin.sesam.dnv.no
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 1.1 (Windows; I; 32bit)
-
- Dean Schulze <schulze@vega.lpl.arizona.edu> wrote:
- >
- > I recently began moving my C++/C/F77 code from
- >SunOS to Windows NT using VC++4.0/PowerStation4.0. Before
- >doing this I considered using gcc/g77 and chose the
- >Microsoft compilers after being assured by MS that mixed
- >language programming would work with their compilers.
- >After working with their compilers and reading their
- >documentation I now see that their claims to support
- >legacy code with mixed language programming are deceitful.
- >
- > In order to mix Fortran and C/C++ with Microsoft
- >compilers you must wrap your Fortran subroutine/function
- >definitions in Microsoft specific extensions.
-
- What exactly do you mean ? I have done almost exactly what you
- describe using MS VC++ and Powerstation, and found that
- it was relatively easy. I did not have to modify a single
- characterof my FORTRAN legacy code.
-
- What is more problematic, is to do portable, mixed language
- programming in C++/F77 (i.e. portable between NT, UNIX and
- VMS).
-
- I believe I have solved this problem also, in such a way
- that my C++ code is unchanged across platforms (not even
- using #defines in C++ code).
- My legacy FORTRAN code is of course not touched.
-
-
- >This means
- >a lot of recoding, adding non-standard code to my standard
- >code. What makes this even worse is that Powerstation
- >Fortran apparently will not invoke the C Preprocessor
- >so I cannot do something like
- >
- >#ifdef MSFORTRAN
- >
- > (Microsoft abortions here)
- >
- >#endif
- >
- >so that I can move the code to a non-Microsoft Fortran
- >compiler.
-
- I don't think you need recoding.
-
- Why would you want to do something like that when
- a) you don't have to
- b) it's complicated
- c) it's a lot of work
- d) it messes up your FORTRAN code in a non-portable way
-
- > Because of this stupidity by Microsoft I STRONGLY
- >RECOMMEND AGAINST DOING MIXED LANGUAGE PROGRAMMMING
- >WITH MICROSOFT COMPILERS. You can get it to work if you
- >write enough MS specific code, but there is no good reason
- >to do this. Consider using gcc/g77 instead under WinNT/95
- >or switching to Linux.
-
- What has hit you .... ? As I see it, mixed language
- programming is EASIEST under MS VC++/Powerstation, as you
- can call the subroutines exactly as expected by a F77
- programmer. The only non-trivial problem you must solve
- is fortran CHARACTERs. You need to write a minimal CHARACTER
- class in C++. You also need to:
-
- typedef int INTEGER;
- typedef float REAL;
- typedef int LOGICAL;
- #include "character.h" // the CHARACTER class
- #define SUBROUTINE extern "C" void __stdcall
- #define INTEGER_FUNCTION extern "C" INTEGER __stdcall
- #define REAL_FUNCTION extern "C" REAL __stdcall
-
- now you can declare FORTRAN subroutine prototypes in C++:
-
- SUBROUTINE MYSUB(INTEGER& I1, CHARACTER C1, INTEGER& I2);
-
- >
- > Mixed language programming is inherently non-portable,
- >but it is not hard to do and there is no good reason for
- >the stupidity that Microsoft has introduced.
- >
- > Microsoft might end up making a Linux user out of
- >me yet.
- >
- >Dean Schulze
- >
-
- Before you switch to Linux:
- My solution is portable (also to SGI), because I did the
- following:
-
- a) I wrote header files for my FORTRAN libraries as described
- above.
- b) Compiled FORTRAN code with Fortran powerstation and
- created *.lib files.
- c) Included the *.lib files in my VC++ project
- d) Included the required header files in my C++ code.
- e) Compiled & linked: no problem
- d) Execution : no problem
-
- porting to Unix:
-
- a) Left C++ and Fortran code untouched !!
- b) Wrote a small C++ program that read my FORTRAN library
- header files, and generated:
- b.1: a platform-specific header file for the fortran
- library. Here you deal with upper/lowercase names,
- trailing underscores, character lengths etc.
- b.2: a platform-specific *.CPP file containing inline
- functions that convert from
- MYSUB(INTEGER& I1, CHARACTER C1, INTEGER& I2)
- to
- mysub_(int& I1, char* C1.rep, int& I2, int C1.len)
-
- (example above is for for SGI)
-
- >
- >P.S. I have written a tutorial introduction to mixed
- >language programming (C++/C/Fortran), including example
- >code, which I'll be glad to send to anyone who wants it.
- >Just e-mail me.
- >
- > My examples are work with Sun and SGI compilers,
- >and will probably work without modifications for
- >gcc/g77 on Win32. My examples show how easy and clean
- >mixed language programming is. No Microsoft-like abortions
- >are needed, and they should be avoided.
- >
-
-
- Please send me your tutorial introduction, and if possible,
- tell me where my strategy fails.
-
- I would actually like to hear if there is more to this
- problem than what I have realised so far, but I would be
- surprised if there is.
-
- Carsten Arnholm
- ca@sesam.dnv.no
-
-